package com.gopay.bis.manager.bank.ebank;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.boc.ebank.BocB2cMessage;
import com.gopay.common.domain.bank.boc.ebank.BocDayEndMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @BocB2cManager.java
 * @author wanggang
 * @2013-11-4 下午2:35:38  www.gopay.com.cn Inc.All rights reserved.
 */
public class BocB2cManager implements BankManagerRemoteService{
	
    @Resource(name = "bocConfig")
    private Map<String, String> bocConfig;
    
    @Resource(name = "bocB2cBankServiceClient")
    private BankService bocB2cBankServiceClient;
    
    @Resource(name = "bocB2cBankServiceClientHna")
    private BankService bocB2cBankServiceClientHna;
    
    /**
	 * B2C签名
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object sign(BaseMessage msg) throws GopayException {		
		BocB2cMessage message = (BocB2cMessage) msg;
		// 交易金额保留小数点后两位(中行要求)
        DecimalFormat df = new DecimalFormat("0.00");
        String amount = df.format(message.getAmount());
        message.setAmount(new BigDecimal(amount));
        message.setCurCode((String) bocConfig.get("BOC.CURTYPE.CNY"));
        message.setMerchantNo(message.getMerID2nd());
        // 中行借贷分离判断
        if (!message.isSupportCredit()) {
            message.setMerchantNo(message.getMerID());
        }
        message.setOrderUrl(message.getPayRuturnUrl());
        message.setBankUrl(msg.getPayUrl());
        message.setOrderNote("gopay");
        message.setPayType(1);
        message.setPlainMessage(buildPlain(message));
    	BankLogUtils.printB2CManagerSignSendLog(message);
    	String signStr;
    	if(message.isHna()){
    		signStr = (String) this.bocB2cBankServiceClientHna.sign(message);
	    }else{
           signStr = (String) bocB2cBankServiceClient.sign(message);
	    }
        message.setSignMessage(signStr);
    	BankLogUtils.printB2CManagerSignReturnLog(message);

        return message;
    }
	
	/**
	 * B2C验签
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		BocB2cMessage message = (BocB2cMessage) msg;
		StringBuffer sb = new StringBuffer("");
        sb.append(message.getMerchantNo()).append("|");
        sb.append(message.getOrderId()).append("|");
        sb.append(message.getOrderSeq()).append("|");
        sb.append(message.getCardTyp()).append("|");
        sb.append(message.getPayTime()).append("|");
        sb.append(message.getOrderStatus()).append("|");
        sb.append(message.getPayAmount());
        message.setPlainMessage(sb.toString());
    	BankLogUtils.printB2CManagerVerifySendLog(message);
    	boolean flag;
    	if(message.isHna()){
    		flag = (Boolean) this.bocB2cBankServiceClientHna.verify(message);
	    }else{
            flag = (Boolean) bocB2cBankServiceClient.verify(message);
	    }
    	BankLogUtils.printB2CManagerVerifyReturnLog(message, flag);
        return flag;
	}
	/**
	 * B2C订单查询
	 * 
	 * @param message
	 * @return
	 */
	@Override
	public Object query(Object obj) throws GopayException {
		BocB2cMessage message = (BocB2cMessage) obj;
        message.setMerchantNo(message.getMerID2nd());
        // 中行对账借贷分离判断
        if (!message.isSupportCredit()) {
            message.setMerchantNo(message.getMerID());
        }
        message.setBankUrl(message.getQueryUrl());
    	BankLogUtils.printB2CManagerQuerySendLog(message, "");
    	if(message.isHna()){
    		message = (BocB2cMessage) this.bocB2cBankServiceClientHna.query(message);
	    }else{
            message = (BocB2cMessage) bocB2cBankServiceClient.query(message);
	    }
    	BankLogUtils.printB2CManagerQueryReturnLog(message);
        return message;
	}
	
	 /**
     * B2C签名明文
     * @param message
     * @return
     */
    private String buildPlain(BocB2cMessage message) {
        StringBuffer sb = new StringBuffer();

        sb.append(StringUtils.trimToEmpty(message.getOrderId())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getOrderDate())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getCurCode())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getAmount().toString())).append("|");
        sb.append(StringUtils.trimToEmpty(message.getMerchantNo()));

        return sb.toString();
    }
    
    /**
     * 日终信息拼装
     * @author guomuye
     * @param base dayEndMsg : merchantNo fileDate ticketUrl downFileUrl
     * @throws Exception 
     */
	public Object dayend(BaseMessage base) throws GopayException {
		BocDayEndMessage dayEndMsg = new BocDayEndMessage();
		BeanUtils.copyProperties(base, dayEndMsg);
		dayEndMsg.setMerchantNo(bocConfig.get("BOC.B2C.MERCHANTNO"));
		dayEndMsg.setMerID(bocConfig.get("BOC.B2C.MERCHANTNO"));
		dayEndMsg.setTicketUrl(bocConfig.get("BOC.B2C.GETTICKETURL"));
		dayEndMsg.setDownFileUrl(bocConfig.get("BOC.B2C.DOWNFILEURL"));
		dayEndMsg.setOutStlmCd("BOC");
		BankLogUtils.printB2CManagerDayendReqLog(dayEndMsg);
		@SuppressWarnings("unchecked")
		List<BaseDayendEntry> dayendList = (List<BaseDayendEntry>) bocB2cBankServiceClient.dayend(dayEndMsg);
		dayEndMsg .setDayendList(dayendList);
		BankLogUtils.printB2CManagerDayendReturnLog(dayEndMsg);
		return dayEndMsg;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

    
}
